#!/sbin/openrc-run
# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/app-emulation/xen-tools/files/xenstored.initd,v 1.2 2011/04/05 21:25:03 alexxy Exp $

depend() {
	before xendomains xend sshd ntp-client ntpd nfs nfsmount rsyncd portmap dhcp
	after localmount
}

check_xenstored_up() {
	test -f /run/xenstored.pid
	return $?
}

wait_xenstored_up() {
	local time=0 timeout=30
	while ! check_xenstored_up; do
		time=$(($time+1))
		if [ $time -gt $timeout ]; then
			return 1
		fi
		sleep 1
	done
	return 0
}

start() {
	checkpath --directory --mode 755 /run/xen
	if check_xenstored_up; then
		ewarn "Xenstore can only be started once"
		return 0
	fi
	local _traceopt=

	ebegin "Starting xenstored daemon"

	if ! test -f /proc/xen/capabilities && \
	   ! grep '^xenfs ' /proc/mounts >/dev/null;
	then
		mount -t xenfs xenfs /proc/xen
	fi

	modprobe xen-evtchn 2>/dev/null
	modprobe xen-gntdev 2>/dev/null
	modprobe evtchn 2>/dev/null
	modprobe gntdev 2>/dev/null

	if yesno "$XENSTORED_TRACE"; then
		_traceopt="-T /var/log/xen/xenstored-trace.log"
	fi

	if yesno "$XENSTORED_STUBDOM"; then
		/usr/bin/init-xenstore-domain /usr/lib/xen/boot/xenstore-stubdom.gz \
			$XENSTORED_STUBDOM_MEMORY \
			$XENSTORED_FLASK_LABEL
		wait_xenstored_up
	else
		start-stop-daemon --start --exec /usr/sbin/xenstored \
			--pidfile /run/xenstored.pid \
			-- --pid-file=/run/xenstored.pid \
			$XENSTORED_OPTS $_traceopt
		wait_xenstored_up
	fi
	eend $? || return 1

	ebegin "Setting domain0 name and domid record"
	/usr/lib/xen/bin/xen-init-dom0
	eend $?
}

stop() {
	ewarn "Xenstore can not be stopped"
}

